package com.fooddiary.web;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.fooddiary.service.IUserService;
import com.fooddiary.util.AppException;
import com.fooddiary.web.form.RegistrationForm;
import com.fooddiary.web.form.UserProfileForm;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Controller
@SessionAttributes("registrationForm")
public class UserController extends AbstractController{
	
	private final String REGISTER_INIT_ACTION = "/public/registration/init.action";
	private final String REGISTER_SUBMIT_ACTION = "/public/registration/submit.action";
	private final String USERPROFILE_INIT_ACTION = "/private/userprofile/init.action";
	private final String USERPROFILE_SUBMIT_ACTION = "/private/userprofile/submit.action";
	
	private final String REGISTRATION_FORM_KEY = "registrationForm";
	private final String USERPROFILE_FORM_KEY = "userProfileForm";
	
	private final String REGISTRATION_URL = "/public/registration";
	private final String REGISTRATION_SUCCESS_URL =	"/public/registrationSuccess";
	private final String USERPROFILE_URL = "/private/userprofile";
	private final String USERPROFILE_SUCCESS_URL = "/private/userprofileSuccess";
	
	@Autowired
	IUserService userService;
	
	@RequestMapping(value=REGISTER_INIT_ACTION, method=RequestMethod.GET)
	public String initRegistrationForm(Model model){
		RegistrationForm registrationForm = new RegistrationForm();
		model.addAttribute( REGISTRATION_FORM_KEY , registrationForm);
		return REGISTRATION_URL;
	}
	
	@RequestMapping(value=REGISTER_SUBMIT_ACTION, method=RequestMethod.POST)
	public ModelAndView submitRegistrationForm(@ModelAttribute(REGISTRATION_FORM_KEY) RegistrationForm registrationForm,
			BindingResult result, SessionStatus status){
		ModelAndView mv = null;
		try {
			registrationForm = userService.registerNewUser( registrationForm );
			if(CollectionUtils.isEmpty( registrationForm.getErrors() )){
				mv = new ModelAndView( REGISTRATION_SUCCESS_URL );
			}else{
				mv = new ModelAndView( REGISTRATION_URL );
			}
		} catch ( Exception err ) {
			mv = new ModelAndView( ERROR_URL );
		}finally{
			mv.addObject( REGISTRATION_FORM_KEY , registrationForm );
		}
		return mv;
	}
	
	@RequestMapping(value=USERPROFILE_INIT_ACTION, method=RequestMethod.GET)
	public ModelAndView initUserProfileForm(Model model){
		UserProfileForm userProfileForm = new UserProfileForm();
		ModelAndView mv = null;
		try {
			userProfileForm = userService.initUserProfileForm();
			mv = new ModelAndView(USERPROFILE_URL);
		} catch ( AppException err ) {
			userProfileForm.getErrors().add( err.getMessage() );
		}finally{
			mv.addObject( USERPROFILE_FORM_KEY, userProfileForm );
		}
		return mv;
	}
	
	@RequestMapping(value=USERPROFILE_SUBMIT_ACTION, method=RequestMethod.POST)
	public ModelAndView submitUserProfileForm(@ModelAttribute(USERPROFILE_FORM_KEY) UserProfileForm userProfileForm,
			BindingResult result, SessionStatus status){
		ModelAndView mv = null;
		try {
			userProfileForm = userService.saveUserProfile( userProfileForm );
			if(CollectionUtils.isEmpty( userProfileForm.getErrors() )){
				mv = new ModelAndView( USERPROFILE_SUCCESS_URL );
			}else{
				mv = new ModelAndView( USERPROFILE_URL );
			}
		} catch ( Exception err ) {
			mv = new ModelAndView( ERROR_URL );
		}finally{
			mv.addObject( USERPROFILE_FORM_KEY , userProfileForm );
		}
		return mv;
	}
}
